﻿layui.use(['form', 'layedit', 'layer'], function () {

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery, player = parent.layer || layui.layer;

    var authorityButtonsAddLogic = {
        //初始化
        init: function () {

            self = this;

            this.Parameters = {
                saveUrl: "/Authority/Buttons/AddInfo",
                authorizationCodeListInfoUrl: "/Authority/Buttons/LoadAuthorizationCodeListInfo",
            };

            this.initPage();

            this.bindEvent();

        },
        //初始化列表
        initPage: function () {

            //获取按钮授权编码列表
            self.logicFunc.loadAuthorizationCodeInfo();
        },
        //绑定事件
        bindEvent: function () {

            //监听提交
            form.on('submit(addButton)', self.logicFunc.saveInfo);

            //关闭
            $("#btnClose").on("click", self.logicFunc.close);
        },
        //逻辑方法
        logicFunc: {

            //获取按钮授权编码列表
            loadAuthorizationCodeInfo: function () {

                GrdLayUI.ajaxPost({
                    url: self.Parameters.authorizationCodeListInfoUrl,
                    data: "",
                    success: function (result) {
                        if (result.Code == 1000) {
                            var data = result.Data;
                            $("#sAuthorizationCode").empty();
                            for (var i = 0; i < data.length; i++) {
                                $("#sAuthorizationCode").append("<option value=" + data[i].Value + ">" + data[i].Text + "</option>");  
                                //$('#sAuthorizationCode').append(new Option(data[i].Value, data[i].Text));// 下拉菜单里添加元素
                            }
                            form.render();
                        } 
                    }
                });
            },

            ///保存数据
            saveInfo: function () {

                var json = {
                    parameters: GrdLayUI.initParamsData($("#add-form"))
                };
                GrdLayUI.ajaxPost({
                    url: self.Parameters.saveUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {

                            GrdLayUI.msg(result.Msg, "成功", function () {
                                //获取窗口索引
                                var index = player.getFrameIndex(window.name);
                                //关闭子页面
                                player.close(index);
                                //刷新父页面
                                window.parent.location.reload();
                            });

                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            },
            //取消
            close: function () {
                player.closeAll();
            }
        },
        //工具
        tools: {
        }
    };
    authorityButtonsAddLogic.init();
});
